<!--#include file="common/header.html"-->
<link rel="stylesheet" type="text/css" href="../css/autocomplete.css" media="all">
<body>
    <div class="container-fluid larry-wrapper">
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <!--头部搜索-->
                <section class="panel panel-padding">
                    <form class="layui-form" action="/education/admin/statsRebate">
                        <div class="layui-form">
                            <div class="layui-inline">
                                <div class="layui-input-inline">
                                    <input class="layui-input" name="_query.keyword" placeholder="关键字">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button lay-submit class="layui-btn" lay-filter="search">查找</button>
                            </div>
                        </div>
                    </form>
                </section>

                <!--列表-->
                <section class="panel panel-padding">
                    <div style="text-align: right" id="totalMsg"></div>
                    <div id="list" class="layui-form"></div>
                    <div class="text-right" id="page"></div>
                </section>
            </div>
        </div>
    </div>

    <div class="add-subcat">
        <form id="form1" class="layui-form layui-form-pane"  action="/education/admin/user/save">
            <input type="hidden" name="id">
            <div class="layui-form-item">
                <label class="layui-form-label">账号</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" autocomplete="off" class="layui-input "
                           required jq-verify="required">
                </div>
                <label class="layui-form-label">用户上线</label>
                <div class="layui-input-inline">
                    <input type="text" name="referee_name" autocomplete="off" class="layui-input " disabled readonly>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">第三方登录号</label>
                <div class="layui-input-inline">
                    <input type="text" name="openid" autocomplete="off" class="layui-input ">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="password" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux" style="font-size: inherit">为空则不修改密码</div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">昵称</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" placeholder="" autocomplete="off" class="layui-input ">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">所属角色</label>
                <div id="formRoleId">
                    <!--<select name="role_id" id="roleId2">
                        <option>用户类型</option>
                    </select>-->
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-inline">
                    <input type="radio" name="sex" title="男" value="1" checked />
                    <input type="radio" name="sex" title="女" value="0" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">生日</label>
                <div class="layui-input-inline">
                    <input type="text" name="birthday" placeholder="" autocomplete="off" class="layui-input"
                           onclick="layui.laydate({elem: this, max: laydate.now()})">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">上传头像</label>
                <div class="layui-input-block">
                    <input type="file" name="file" class="layui-upload-file"  id="img">
                    <input type="hidden" name="img"  error-id="img-error">
                    <p id="img-error" class="error"></p>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-inline">
                    <input type="radio" name="status" title="启用" value="1" checked />
                    <input type="radio" name="status" title="禁用" value="0" />
                </div>
            </div>

            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">个性签名</label>
                <div class="layui-input-block">
                    <textarea name="description"  class="layui-textarea"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">充值余额</label>
                <div class="layui-input-inline">
                    <input type="text" name="money" placeholder="" autocomplete="off" class="layui-input " disabled
                           required jq-verify="required">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">赠送金额</label>
                <div class="layui-input-inline">
                    <input type="text" name="gift" placeholder="" autocomplete="off" class="layui-input " disabled
                           required jq-verify="required">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">积分</label>
                <div class="layui-input-inline">
                    <input type="text" name="point" placeholder="积分" autocomplete="off" class="layui-input" value="0">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">支付密码</label>
                <div class="layui-input-inline">
                    <input type="text" name="pay_pass" placeholder="支付密码" autocomplete="off" class="layui-input"
                           value="" required jq-verify="required">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" jq-submit jq-filter="submit">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>

</body>
<!--#include file="stats-rebate-list.html"-->
<script src="../js/layui/layui.js"></script>
<script type="text/javascript" src="../js/lib/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="../js/lib/rest.js"></script>
<script type="text/javascript" src="../js/lib/bootstrap.autocomplete.js"></script>
<!--#include file="common/version.html"-->
<script>
    layui.use('user_list');

    function initData(){
        $.ajax({
            dataType:'json',
            method:'post',
            url: '/education/admin/role',
            data: {},
            async: false,
            success: function (rs) {
                if (rs.status == 200) {
                    var arr = ['<option value="">用户类型</option>'];
                    $.each(rs.data.list, function (i, n) {
                        var v ='<option value={v}>{n}</option>'
                            .replace('{v}', n.ids).replace('{n}', n.names);
                        arr.push(v);
                    });
                    $('#roleId').html(arr.join(''));
                    var $str = '';
                    $.each(rs.data.list, function (i, n) {
                        var v = '<input type="radio" name="role_id" title="{n}" value="{v}" disabled/>';
                        $str += v.replace('{v}', n.ids).replace('{n}', n.names);
                    });
                    $('#formRoleId').html($str);

                    $str = '';
                    $.each(rs.data.list, function (i, n) {
                        var v = '<input type="radio" name="role_id" title="{n}" value="{v}"/>';
                        $str += v.replace('{v}', n.ids).replace('{n}', n.names);
                    });
                    $('#changeRoleId').html($str);
                }
            }
        });
    }


    function afterTableLoad(data) {
        var str = '返利总额：{a}万元';
        str = str
            .replace(/{a}/, ((data.extData.total||0)/10000).toFixed(2))
        $('#totalMsg').html(str)
        layui.use(['jquery', 'jqmenu'], function(exports) {
            var jqmenu = layui.jqmenu;
            var menu = new jqmenu();
            menu.menuBind();
        });
    }

    function beforeUpdate(obj){
        var json = $(obj).data('params');
        var ids = json.key.split('=')[1];
        $.post('/education/admin/user/view/' + ids, null, function(rs){console.log(rs)
            //清空原有数据
            $('.imgbox',$('#form1')).remove();
            var d = rs.data;
            //设置图片
            $.each(d, function(i, n){
                if(n && new String(n).indexOf('/files/upload') > -1){
                    _showImg(i, n);
                }
            });
            refreshImg();
        })
    }

    function refreshImg(){
        layui.use('layer', function(){
            var layer = layui.layer;
            layer.photos({
                photos: '.layer-photos-demo'
                ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
            });
        });
    }

    function _edit(ids) {
        var index = layer.open({
            type: 1,
            title:'编辑',
            content: $('.add-subcat')
        });
        layer.full(index);
        $('.imgbox').children().remove();
        $('#form1').attr('action', '/education/admin/user/update/' + ids);
        restGet('/education/admin/user/view/' + ids, null, $('#form1'), function(rs){
            if(rs.status == 200 || rs.code == 200){console.log(rs)
                var str =  '<img src="/education{src}" alt="..." class="img-thumbnail" style="width: 200px;">';
                var imgDiv = $('input[name=img]').parent().next().children('.imgbox');
                imgDiv.append(str.replace('{src}', rs.data.img));
            }
        })
    }

    var _showImg = function(k, q){
        var arr = q.split(',');
        var content ='';
        for(var i=0; i< arr.length; i++){
            var _url = arr[i];
            var str =  '<img name="file" src="{src}" class="img-thumbnail" style="height:200px;">';
            str = str.replace(/{src}/g, '/education'+_url);
            str = '<span class="layer-photos-demo">' + str + '</span>';
          //  str += '<a style="cursor: pointer;margin-right: 50px;" onclick="delImg(this)"><span class="layui-icon" style="color: red;font-size: xx-large;vertical-align:top;">&#x1006;</span></a>'
            content += '<span>' + str + '</span>';
        }
        var rs = '<div class="imgbox" style="height: 200px;">'+content+'</div>';
        $('input[name='+k+']').parent().after(rs);
    };

    function delImg(obj){
        var input = $(obj).parent().parent().prev().find('input[type=hidden]');
        if($(obj).parent().parent().children('span').length == 1){
            input.val('');
            input.prev().val('');//两张图片的处理
            $(obj).parent().parent().remove();
        } else {
            var v = $(obj).prev().find('img').attr('src').replace('/education', '');
            //先处理两张图片的情况
            if(input.prev('input').length >0){
                if(v == input.prev('input').val()){
                    input.prev('input').val('');
                }else{
                    input.val('');
                }
            } else {
                var s = input.val().replace(','+ v, '')
                        .replace(v +',', '');
                input.val(s);
            }
            $(obj).parent().remove();
        }
    }

    function closeTab(){
        alert(0)
    }

    function _gotoOrder(ids, mobile){
        var index = layer.open({
            type: 2,
            content: "order.html?userid=" + ids, "title": '【'+ mobile +"】订单管理", "full":"true"
        });
        layer.full(index);
    }

</script>
<script>
    function beforeSubmit() {
        if($('#form4').attr('action').indexOf('transfer') > 0) {
            if(!$('#addUserid').val()) {
                layer.msg('请选择需要操作的记录');
                return false;
            }
        }
    }
    function beforeAdd(obj){
        $('#forceDiv').show();
        $('#addUserid').val($('input[name="_query.userid"]').val());
    }
    function beforeTransfer(obj){
        $('#forceDiv').hide();
        var arr = [];
        $("input[name=checkbox]:checked").each(function() {
            arr.push($(this).val());
        });
        var vals = arr.toString();
        if (vals == "") {
            layer.msg('请选择需要操作的记录');
            return;
        }
        $('#addUserid').val(vals);
    }
    $(function(){
        $('#autocompleteInput').autocomplete({
            source:function(query,process){
                var matchCount = this.options.items;//返回结果集最大数量
                $.post("/education/admin/user/query",{"keyword":query, },function(respData){
                    console.log(respData);
                    if(respData.code == 200) {
                        return process(respData.data);
                    }
                });
            },
            formatItem:function(item){
                var name = item["name"] || '';
                var username = item["username"] || '';
                return name +"("+ username + ")";
            },
            setValue:function(item){
                return {'data-value':item["username"],'real-value':item["username"]+'-'+item["id"]};
            }
        });
    });
</script>
</html>